﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;


namespace dbPoll_Application.Models
{
    public enum Gender
    {
        Unknown = 0,
        Male = 1,
        Female = 2
    }

    public class User
    {
        public virtual String ProviderName { get; set; }
        public virtual String UserName { get; set; }
        public virtual String Password { get; set; }
        public virtual object ProviderUserKey { get; set; }
        public virtual String Email { get; set; }
        public virtual String PasswordQuestion { get; set; }
        public virtual String PasswordAnswer { get; set; }
        public virtual String Comment { get; set; }
        public virtual Boolean IsApproved { get; set; }
        public virtual Boolean IsLockedOut { get; set; }
        public virtual Boolean IsGeneric { get; set; }
        public virtual Boolean IsDeleted { get; set; }
        public virtual DateTime CreationDate { get; set; }
        public virtual DateTime LastLoginDate { get; set; }
        public virtual DateTime LastActivityDate { get; set; }
        public virtual DateTime LastPasswordChangedDate { get; set; }
        public virtual DateTime LastLockoutDate { get; set; }
        public virtual DateTime AccountExpiryDate { get; set; }
        public virtual int FailedPasswordAttemptCount { get; set; }
        public virtual DateTime FailedPasswordAttemptWindowStart { get; set; }
        public virtual int FailedPasswordAnswerAttemptCount { get; set; }
        public virtual DateTime FailedPasswordAnswerAttemptWindowStart { get; set; }
        public virtual String LastName { get; set; }
        public virtual String FirstName { get; set; }
        public virtual Gender Gender { get; set; }
        public virtual String Position { get; set; }
        public virtual String City { get; set; }
        public virtual String Country { get; set; }
        public virtual ICollection<Role> Roles { get; set; }
        public virtual IList<Poll> CreatedPolls { get; set; }
        public virtual IList<Poll> ManagedPolls { get; set; }
        public virtual ICollection<GenericEmail> GenericEmails { get; set; }
        public virtual String Organisation { get; set; }
        public virtual ICollection<ClientCode> ClientCodes { get; set; }

        public User()
        {
            this.ProviderName = String.Empty;
            this.UserName = String.Empty;
            this.Password = String.Empty;
            this.ProviderUserKey = null;
            this.Email = String.Empty;
            this.PasswordQuestion = String.Empty;
            this.PasswordAnswer = String.Empty;
            this.Comment = String.Empty;
            this.IsApproved = false;
            this.IsLockedOut = false;
            this.IsGeneric = false;
            this.IsDeleted = false;
            this.CreationDate = DateTime.Now;
            this.LastLoginDate = DateTime.Now;
            this.LastActivityDate = DateTime.Now;
            this.LastPasswordChangedDate = DateTime.Now;
            this.LastLockoutDate = DateTime.Now;
            this.FailedPasswordAttemptCount = 0;
            this.FailedPasswordAttemptWindowStart = DateTime.Now;
            this.FailedPasswordAnswerAttemptCount = 0;
            this.FailedPasswordAnswerAttemptWindowStart = DateTime.Now;
            this.LastName = String.Empty;
            this.FirstName = String.Empty;
            this.Roles = new List<Role>();
            this.CreatedPolls = new List<Poll>();
            this.ManagedPolls = new List<Poll>();
            this.GenericEmails = new List<GenericEmail>();
            this.ClientCodes = new List<ClientCode>();
            this.Gender = Gender.Unknown;
            this.Organisation = String.Empty;
            this.Position = String.Empty;
            this.City = String.Empty;
            this.Country = String.Empty;
        }

        public User(String providername,
            String username,
            String password,
            object providerUserKey,
            String email,
            String passwordQuestion,
            String passwordAnswer,
            String comment,
            Boolean isApproved,
            Boolean isLockedOut,
            DateTime creationDate,
            DateTime lastLoginDate,
            DateTime lastActivityDate,
            DateTime lastPasswordChangedDate,
            DateTime lastLockoutDate,
            String lastName,
            String firstName)
        {
            this.ProviderName = providername;
            this.UserName = username;
            this.Password = password;
            this.ProviderUserKey = providerUserKey;
            this.Email = email;
            this.PasswordQuestion = passwordQuestion;
            this.PasswordAnswer = passwordAnswer;
            this.Comment = comment;
            this.IsApproved = isApproved;
            this.IsLockedOut = isLockedOut;
            this.IsGeneric = false;
            this.IsDeleted = false;
            this.CreationDate = creationDate;
            this.LastLoginDate = lastLoginDate;
            this.LastActivityDate = lastActivityDate;
            this.LastPasswordChangedDate = lastPasswordChangedDate;
            this.LastLockoutDate = lastLockoutDate;
            this.FailedPasswordAttemptCount = 0;
            this.FailedPasswordAttemptWindowStart = DateTime.Now;
            this.FailedPasswordAnswerAttemptCount = 0;
            this.FailedPasswordAnswerAttemptWindowStart = DateTime.Now;
            this.LastName = lastName;
            this.FirstName = firstName;
            this.Roles = new List<Role>();
            this.CreatedPolls = new List<Poll>();
            this.ManagedPolls = new List<Poll>();
            this.GenericEmails = new List<GenericEmail>();
            this.ClientCodes = new List<ClientCode>();
            this.Gender = Gender.Unknown;
            this.Organisation = String.Empty;
            this.Position = String.Empty;
            this.City = String.Empty;
            this.Country = String.Empty;
        }

        public User(String providername,
            String username,
            String password,
            object providerUserKey,
            String email,
            String passwordQuestion,
            String passwordAnswer,
            String comment,
            Boolean isApproved,
            Boolean isLockedOut,
            DateTime creationDate,
            DateTime lastLoginDate,
            DateTime lastActivityDate,
            DateTime lastPasswordChangedDate,
            DateTime lastLockoutDate,
            String lastName,
            String firstName,
            ICollection<Role> roles,
            IList<Poll> createdPolls,
            IList<Poll> managedPolls)
        {
            this.ProviderName = providername;
            this.UserName = username;
            this.Password = password;
            this.ProviderUserKey = providerUserKey;
            this.Email = email;
            this.PasswordQuestion = passwordQuestion;
            this.PasswordAnswer = passwordAnswer;
            this.Comment = comment;
            this.IsApproved = isApproved;
            this.IsLockedOut = isLockedOut;
            this.IsGeneric = false;
            this.IsDeleted = false;
            this.CreationDate = creationDate;
            this.LastLoginDate = lastLoginDate;
            this.LastActivityDate = lastActivityDate;
            this.LastPasswordChangedDate = lastPasswordChangedDate;
            this.LastLockoutDate = lastLockoutDate;
            this.FailedPasswordAttemptCount = 0;
            this.FailedPasswordAttemptWindowStart = DateTime.Now;
            this.FailedPasswordAnswerAttemptCount = 0;
            this.FailedPasswordAnswerAttemptWindowStart = DateTime.Now;
            this.LastName = lastName;
            this.FirstName = firstName;
            this.Roles = roles;
            this.CreatedPolls = createdPolls;
            this.ManagedPolls = managedPolls;
            this.GenericEmails = new List<GenericEmail>();
            this.ClientCodes = new List<ClientCode>();
            this.Gender = Gender.Unknown;
            this.Organisation = String.Empty;
            this.Position = String.Empty;
            this.City = String.Empty;
            this.Country = String.Empty;
        }

        public User(String providername,
            String username,
            String password,
            object providerUserKey,
            String email,
            String passwordQuestion,
            String passwordAnswer,
            String comment,
            Boolean isApproved,
            Boolean isLockedOut,
            DateTime creationDate,
            DateTime lastLoginDate,
            DateTime lastActivityDate,
            DateTime lastPasswordChangedDate,
            DateTime lastLockoutDate,
            int failedPasswordAttemptCount,
            DateTime failedPasswordAttemptWindowStart,
            int failedPasswordAnswerAttemptCount,
            DateTime failedPasswordAnswerAttemptWindowStart,
            String lastName,
            String firstName)
        {
            this.ProviderName = providername;
            this.UserName = username;
            this.Password = password;
            this.ProviderUserKey = providerUserKey;
            this.Email = email;
            this.PasswordQuestion = passwordQuestion;
            this.PasswordAnswer = passwordAnswer;
            this.Comment = comment;
            this.IsApproved = isApproved;
            this.IsLockedOut = isLockedOut;
            this.IsGeneric = false;
            this.IsDeleted = false;
            this.CreationDate = creationDate;
            this.LastLoginDate = lastLoginDate;
            this.LastActivityDate = lastActivityDate;
            this.LastPasswordChangedDate = lastPasswordChangedDate;
            this.LastLockoutDate = lastLockoutDate;
            this.FailedPasswordAttemptCount = failedPasswordAttemptCount;
            this.FailedPasswordAttemptWindowStart = failedPasswordAttemptWindowStart;
            this.FailedPasswordAnswerAttemptCount = failedPasswordAnswerAttemptCount;
            this.FailedPasswordAnswerAttemptWindowStart = failedPasswordAnswerAttemptWindowStart;
            this.LastName = lastName;
            this.FirstName = firstName;
            this.Roles = new List<Role>();
            this.CreatedPolls = new List<Poll>();
            this.ManagedPolls = new List<Poll>();
            this.GenericEmails = new List<GenericEmail>();
            this.ClientCodes = new List<ClientCode>();
            this.Gender = Gender.Unknown;
            this.Organisation = String.Empty;
            this.Position = String.Empty;
            this.City = String.Empty;
            this.Country = String.Empty;
        }

        public User(String username)
        {
            this.ProviderName = String.Empty;
            this.UserName = username;
            this.Password = String.Empty;
            this.ProviderUserKey = null;
            this.Email = String.Empty;
            this.PasswordQuestion = String.Empty;
            this.PasswordAnswer = String.Empty;
            this.Comment = String.Empty;
            this.IsApproved = true;
            this.IsLockedOut = false;
            this.IsGeneric = false;
            this.IsDeleted = false;
            this.CreationDate = DateTime.Now;
            this.LastLoginDate = DateTime.Now;
            this.LastActivityDate = DateTime.Now;
            this.LastPasswordChangedDate = DateTime.Now;
            this.LastLockoutDate = DateTime.Now;
            this.FailedPasswordAttemptCount = 0;
            this.FailedPasswordAttemptWindowStart = DateTime.Now;
            this.FailedPasswordAnswerAttemptCount = 0;
            this.FailedPasswordAnswerAttemptWindowStart = DateTime.Now;
            this.LastName = String.Empty;
            this.FirstName = String.Empty;
            this.Roles = new List<Role>();
            this.CreatedPolls = new List<Poll>();
            this.ManagedPolls = new List<Poll>();
            this.GenericEmails = new List<GenericEmail>();
            this.ClientCodes = new List<ClientCode>();
            this.Gender = Gender.Unknown;
            this.Organisation = String.Empty;
            this.Position = String.Empty;
            this.City = String.Empty;
            this.Country = String.Empty;
        }

        public User(String username, String password)
        {
            this.ProviderName = String.Empty;
            this.UserName = username;
            this.Password = password;
            this.ProviderUserKey = null;
            this.Email = String.Empty;
            this.PasswordQuestion = String.Empty;
            this.PasswordAnswer = String.Empty;
            this.Comment = String.Empty;
            this.IsApproved = true;
            this.IsLockedOut = false;
            this.IsGeneric = false;
            this.IsDeleted = false;
            this.CreationDate = DateTime.Now;
            this.LastLoginDate = DateTime.Now;
            this.LastActivityDate = DateTime.Now;
            this.LastPasswordChangedDate = DateTime.Now;
            this.LastLockoutDate = DateTime.Now;
            this.FailedPasswordAttemptCount = 0;
            this.FailedPasswordAttemptWindowStart = DateTime.Now;
            this.FailedPasswordAnswerAttemptCount = 0;
            this.FailedPasswordAnswerAttemptWindowStart = DateTime.Now;
            this.LastName = String.Empty;
            this.FirstName = String.Empty;
            this.Roles = new List<Role>();
            this.CreatedPolls = new List<Poll>();
            this.ManagedPolls = new List<Poll>();
            this.GenericEmails = new List<GenericEmail>();
            this.ClientCodes = new List<ClientCode>();
            this.Gender = Gender.Unknown;
            this.Organisation = String.Empty;
            this.Position = String.Empty;
            this.City = String.Empty;
            this.Country = String.Empty;
        }

        public User(String username, String password, String passwordQuestion, String passwordAnswer)
        {
            this.ProviderName = String.Empty;
            this.UserName = username;
            this.Password = password;
            this.ProviderUserKey = null;
            this.Email = String.Empty;
            this.PasswordQuestion = passwordQuestion;
            this.PasswordAnswer = passwordAnswer;
            this.Comment = String.Empty;
            this.IsApproved = true;
            this.IsLockedOut = false;
            this.IsGeneric = false;
            this.IsDeleted = false;
            this.CreationDate = DateTime.Now;
            this.LastLoginDate = DateTime.Now;
            this.LastActivityDate = DateTime.Now;
            this.LastPasswordChangedDate = DateTime.Now;
            this.LastLockoutDate = DateTime.Now;
            this.FailedPasswordAttemptCount = 0;
            this.FailedPasswordAttemptWindowStart = DateTime.Now;
            this.FailedPasswordAnswerAttemptCount = 0;
            this.FailedPasswordAnswerAttemptWindowStart = DateTime.Now;
            this.LastName = String.Empty;
            this.FirstName = String.Empty;
            this.Roles = new List<Role>();
            this.CreatedPolls = new List<Poll>();
            this.ManagedPolls = new List<Poll>();
            this.GenericEmails = new List<GenericEmail>();
            this.ClientCodes = new List<ClientCode>();
            this.Gender = Gender.Unknown;
            this.Organisation = String.Empty;
            this.Position = String.Empty;
            this.City = String.Empty;
            this.Country = String.Empty;
        }

        public User(object providerUserKey,
            String username, 
            String password, 
            String email, 
            String lastName, 
            String firstName, 
            DateTime expire, 
            String passwordQuestion, 
            String passwordAnswer)
        {
            this.ProviderName = String.Empty;
            this.UserName = username;
            this.Password = password;
            this.ProviderUserKey = null;
            this.Email = email;
            this.PasswordQuestion = passwordQuestion;
            this.PasswordAnswer = passwordAnswer;
            this.Comment = String.Empty;
            this.IsApproved = true;
            this.IsLockedOut = false;
            this.IsGeneric = false;
            this.IsDeleted = false;
            this.CreationDate = DateTime.Now;
            this.LastLoginDate = DateTime.Now;
            this.LastActivityDate = DateTime.Now;
            this.LastPasswordChangedDate = DateTime.Now;
            this.LastLockoutDate = DateTime.Now;
            this.FailedPasswordAttemptCount = 0;
            this.FailedPasswordAttemptWindowStart = DateTime.Now;
            this.FailedPasswordAnswerAttemptCount = 0;
            this.FailedPasswordAnswerAttemptWindowStart = DateTime.Now;
            this.LastName = lastName;
            this.FirstName = firstName;
            this.Roles = new List<Role>();
            this.CreatedPolls = new List<Poll>();
            this.ManagedPolls = new List<Poll>();
            this.GenericEmails = new List<GenericEmail>();
            this.ClientCodes = new List<ClientCode>();
            this.Gender = Gender.Unknown;
            this.Organisation = String.Empty;
            this.Position = String.Empty;
            this.City = String.Empty;
            this.Country = String.Empty;
        }

        public virtual void Populate(String firstName, String lastName, String organisation, String position, String city, Gender gender, ClientCode clientCode)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Organisation = organisation;
            this.Position = position;
            this.City = city;
            this.Gender = gender;
            this.ClientCodes.Add(clientCode);
        }

        public virtual MembershipUser ToMembershipUser()
        {
            return new MembershipUser(this.ProviderName,
                                                  this.UserName,
                                                  this.ProviderUserKey,
                                                  this.Email,
                                                  this.PasswordQuestion,
                                                  this.Comment,
                                                  this.IsApproved,
                                                  this.IsLockedOut,
                                                  this.CreationDate,
                                                  this.LastLoginDate,
                                                  this.LastActivityDate,
                                                  this.LastPasswordChangedDate,
                                                  this.LastLockoutDate);
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            var user = obj as User;
            if ((System.Object)user == null)
            {
                return false;
            }
            return this.UserName == user.UserName;
        }

        public override int GetHashCode()
        {
            return this.UserName.GetHashCode();
        }
    }
}
